package com.wgy.blog.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wgy.blog.common.Result;
import com.wgy.blog.mapper.LabelMapper;
import com.wgy.blog.pojo.Label;
import com.wgy.blog.pojo.PageQuery;
import com.wgy.blog.service.LabelService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class LabelServiceImpl implements LabelService {

    @Resource
    private LabelMapper labelMapper;


    @Override
    public Result<Integer> add(Label label) {
        int insert = labelMapper.insert(label);
        if (insert>0){
            return Result.addSuccess();
        }
        return Result.addFail();
    }

    @Override
    public Result<Integer> update(Label label) {
        int i = labelMapper.updateByPrimaryKey(label);
        if (i>0){
            return Result.success();
        }
        return Result.fail();
    }

    @Override
    public Result<Integer> delete(Long labelId) {
        int i = labelMapper.deleteByPrimaryKey(labelId);
        if (i>0){
            return Result.success();
        }
        return Result.fail();
    }

    @Override
    public Result<List<Label>> findAll(PageQuery pageQuery) {
        PageHelper.startPage(pageQuery.getPageNum(), pageQuery.getPageSize());
        List<Label> all = labelMapper.findAll();
        PageInfo<List<Label>> pageInfo = new PageInfo(all);
        return Result.success((int) pageInfo.getTotal(),all);
    }

    @Override
    public Result<List<Label>> find() {
        List<Label> labelList = labelMapper.find();
        return Result.success(labelList);
    }
}
